#include <ctime>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <cassert>

using std::cout;
using std::endl;
using std::vector;

// This function converts @number based on ten to the same value based on @base
// with minimum digit as @min_digit. The resulted digits are stored in @result.
void ConvertToDifferentBase(const unsigned int& number,
                            const unsigned int& base,
                            const int& min_digit,
                            vector<int> *result) {
  assert(base >= 2);
  assert(min_digit + base > 1);
  result->clear();
  int carry = 0;
  unsigned int n = number;
  while (n > 0) {
    int r = n % base + carry;
    if (r >= min_digit + base) {
      carry = 1;
      r -= base;
    } else {
      carry = 0;
    }
    result->push_back(r);
    n /= base;
  }
  result->push_back(carry);
}

void Validate(const unsigned int& number,
              const unsigned int& base,
              const int& min_digit,
              const vector<int>& result) {
  int value = 0;
  vector<int>::const_iterator it = result.begin();
  for (unsigned int power = 1; it != result.end(); ++it) {
    value += *it * power;
    power *= base;
  }
  cout << "number: " << number << endl;
  cout << "result: " << value << endl;
  if (number == value) {
    cout << "Succeed!" << endl;
  } else {
    cout << "Failed!!!!!!!!!" << endl;
    exit(0);
  }
}

int main() {
  srand(time(0));
  vector<int> result;
  for (int i = 0; i < 100; ++i) {
    for (int base = 2; base < 10; ++base) {
      for (int min_digit = 2 - base; min_digit <= 0; ++min_digit) {
        for (int case_num = 0; case_num < 2; ++case_num) {
          unsigned int number = case_num * rand() + (1 - case_num) * i;
          result.clear();
          ConvertToDifferentBase(number, base, min_digit, &result);
          Validate(number, base, min_digit, result);

          // for (vector<int>::const_reverse_iterator it = result.rbegin();
          //      it != result.rend(); ++it) {
          //   cout << *it << " ";
          // }
          // cout << endl;
        }
      }
    }
  }
  return 0;
}
